/*
 * @Author: LiZhiWei
 * @Date: 2025-07-15 10:19:07
 * @LastEditors: wjc
 * @LastEditTime: 2025-10-28 11:50:32
 * @Description: 删除数据 Hook - 业务逻辑直接写入函数体
 */
import { api, Http } from '#/api'
import { ElMessage, ElMessageBox } from 'element-plus'

export function useDestroy(
  options: {
    entity?: string
    list?: Ref<any[]>
    message?: string
  } = {
    entity: '',
    message: '删除成功',
  }
) {
  const { entity = '', list, message } = options
  const formLoading = ref(false)

  function destroy(url: string, data: any, tip = '确定删除所选数据吗？') {
    if (formLoading.value) return Promise.reject(new Error('操作进行中'))
    formLoading.value = true

    return ElMessageBox.confirm(tip, '温馨提示', { type: 'warning' })
      .then(() => {
        return Http.Delete(url, data)
          .then((res: any) => {
            ElMessage({ type: 'success', message: message ?? '删除成功' })
            return res
          })
          .finally(() => {
            formLoading.value = false
          })
      })
      .catch(() => {
        formLoading.value = false
        throw new Error('操作已取消')
      })
  }

  const afterDestroyed = (ids: (number | string)[], res: any, onDestroyed?: (res: any) => void) => {
    if (list?.value) {
      list.value = list.value.filter((row) => !ids.includes(row.id))
    }

    onDestroyed && onDestroyed(res)
  }

  const handleDestroy = (id: number | string, onDestroyed?: (res: any) => void) => {
    destroy(api[entity].destroyBatch, { ids: [id] }).then((res) => {
      afterDestroyed([id], res, onDestroyed)
    })
  }

  const handleDestroyWith = (url: RequestUrl, onDestroyed?: (res: any) => void) => {
    destroy(url.destroy as string, url.params).then((res) => {
      afterDestroyed(url.params?.ids, res, onDestroyed)
    })
  }

  const handleDestroyBatch = (ids: Array<number | string>, onDestroyed?: (res: any) => void) => {
    destroy(api[entity].destroyBatch, { ids }, `确定删除选中的 ${ids.length} 条数据吗？`).then(
      (res) => {
        afterDestroyed(ids, res, onDestroyed)
      }
    )
  }

  const handleDestroyBatchWith = (url: RequestUrl, onDestroyed?: (res: any) => void) => {
    destroy(
      url.destroyBatch as string,
      url.params,
      `确定删除选中的 ${url.params?.ids.length} 条数据吗？`
    ).then((res) => {
      afterDestroyed(url.params?.ids, res, onDestroyed)
    })
  }

  const handleForceDestroy = (id: number | string, onDestroyed?: (res: any) => void) => {
    destroy(api[entity].forceDestroy(id), {}, '将永久擦删数据, 不可恢复！是否继续？').then(
      (res) => {
        afterDestroyed([id], res, onDestroyed)
      }
    )
  }

  const handleForceDestroyWith = (url: RequestUrl, onDestroyed?: (res: any) => void) => {
    destroy(url.forceDestroy as string, url.params, '将永久擦删数据, 不可恢复！是否继续？').then(
      (res) => {
        afterDestroyed(url.params?.ids, res, onDestroyed)
      }
    )
  }

  return {
    handleDestroy,
    handleDestroyWith,
    handleDestroyBatch,
    handleDestroyBatchWith,
    handleForceDestroy,
    handleForceDestroyWith,
  }
}
